<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Form 表单 | MyUI 4.x</title>
    <meta name="generator" content="VuePress 1.5.4">
    <link rel="icon" href="/my/favicon.ico">
    <script>
      var _hmt = _hmt || [];
      (function() {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?c4e5f73318b5cb0c389e3d9a05f831cc";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
      })();
    </script>
    <meta name="description" content="MyUI是美亚柏科旗下新德汇出品的Web前端一站式项目工程框架，专注于公检法行业中台系统快速搭建，框架已在多个项目实战检验。">
    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Expires" content="0">
    <link rel="preload" href="/my/assets/css/0.styles.810d4cba.css" as="style"><link rel="preload" href="/my/assets/js/app.2dbd0b81.js" as="script"><link rel="preload" href="/my/assets/js/493.009afff8.js" as="script"><link rel="preload" href="/my/assets/js/1311.77560062.js" as="script"><link rel="preload" href="/my/assets/js/1031.8d6cd2c9.js" as="script">
    <link rel="stylesheet" href="/my/assets/css/0.styles.810d4cba.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/my/" class="home-link router-link-active"><img src="/my/img/logo.png" alt="MyUI 4.x" class="logo"> <span class="site-name can-hide">MyUI 4.x</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/my/guide/" class="nav-link">
  指南
</a></div><div class="nav-item"><a href="/my/ui/components/" class="nav-link">
  组件
</a></div><div class="nav-item"><a href="/my/ui/charts/" class="nav-link">
  图表
</a></div><div class="nav-item"><a href="/my/ui/map/" class="nav-link">
  地图
</a></div><div class="nav-item"><a href="/my/ui/go/" class="nav-link">
  关系图
</a></div><div class="nav-item"><a href="/my/ui/icon/" class="nav-link">
  图标
</a></div><div class="nav-item"><a href="/my/ui/pages/" class="nav-link">
  页面
</a></div><div class="nav-item"><a href="http://newgateway.gitee.io/my-web/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  演示
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="JSDoc" class="dropdown-title"><span class="title">JSDoc</span> <span class="arrow down"></span></button> <button type="button" aria-label="JSDoc" class="mobile-dropdown-title"><span class="title">JSDoc</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/my/api/components.html" class="nav-link">
  基础组件
</a></li><li class="dropdown-item"><!----> <a href="/my/api/utils.html" class="nav-link">
  工具函数
</a></li><li class="dropdown-item"><!----> <a href="/my/api/charts.html" class="nav-link">
  图表组件
</a></li><li class="dropdown-item"><!----> <a href="/my/api/map.html" class="nav-link">
  地图组件
</a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/my/guide/" class="nav-link">
  指南
</a></div><div class="nav-item"><a href="/my/ui/components/" class="nav-link">
  组件
</a></div><div class="nav-item"><a href="/my/ui/charts/" class="nav-link">
  图表
</a></div><div class="nav-item"><a href="/my/ui/map/" class="nav-link">
  地图
</a></div><div class="nav-item"><a href="/my/ui/go/" class="nav-link">
  关系图
</a></div><div class="nav-item"><a href="/my/ui/icon/" class="nav-link">
  图标
</a></div><div class="nav-item"><a href="/my/ui/pages/" class="nav-link">
  页面
</a></div><div class="nav-item"><a href="http://newgateway.gitee.io/my-web/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  演示
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="JSDoc" class="dropdown-title"><span class="title">JSDoc</span> <span class="arrow down"></span></button> <button type="button" aria-label="JSDoc" class="mobile-dropdown-title"><span class="title">JSDoc</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/my/api/components.html" class="nav-link">
  基础组件
</a></li><li class="dropdown-item"><!----> <a href="/my/api/utils.html" class="nav-link">
  工具函数
</a></li><li class="dropdown-item"><!----> <a href="/my/api/charts.html" class="nav-link">
  图表组件
</a></li><li class="dropdown-item"><!----> <a href="/my/api/map.html" class="nav-link">
  地图组件
</a></li></ul></div></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Basic</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>Form</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/my/element/cascader.html" class="sidebar-link">Cascader 级联选择器</a></li><li><a href="/my/element/checkbox.html" class="sidebar-link">Checkbox 多选框</a></li><li><a href="/my/element/color-picker.html" class="sidebar-link">ColorPicker 颜色选择器</a></li><li><a href="/my/element/date-picker.html" class="sidebar-link">DatePicker 日期选择器</a></li><li><a href="/my/element/datetime-picker.html" class="sidebar-link">DateTimePicker 日期时间选择器</a></li><li><a href="/my/element/form.html" aria-current="page" class="active sidebar-link">Form 表单</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/my/element/form.html#典型表单" class="sidebar-link">典型表单</a></li><li class="sidebar-sub-header"><a href="/my/element/form.html#行内表单" class="sidebar-link">行内表单</a></li><li class="sidebar-sub-header"><a href="/my/element/form.html#对齐方式" class="sidebar-link">对齐方式</a></li><li class="sidebar-sub-header"><a href="/my/element/form.html#表单验证" class="sidebar-link">表单验证</a></li><li class="sidebar-sub-header"><a href="/my/element/form.html#自定义校验规则" class="sidebar-link">自定义校验规则</a></li><li class="sidebar-sub-header"><a href="/my/element/form.html#动态增减表单项" class="sidebar-link">动态增减表单项</a></li><li class="sidebar-sub-header"><a href="/my/element/form.html#数字类型验证" class="sidebar-link">数字类型验证</a></li><li class="sidebar-sub-header"><a href="/my/element/form.html#表单内组件尺寸控制" class="sidebar-link">表单内组件尺寸控制</a></li><li class="sidebar-sub-header"><a href="/my/element/form.html#form-attributes" class="sidebar-link">Form Attributes</a></li><li class="sidebar-sub-header"><a href="/my/element/form.html#form-methods" class="sidebar-link">Form Methods</a></li><li class="sidebar-sub-header"><a href="/my/element/form.html#form-events" class="sidebar-link">Form Events</a></li><li class="sidebar-sub-header"><a href="/my/element/form.html#form-item-attributes" class="sidebar-link">Form-Item Attributes</a></li><li class="sidebar-sub-header"><a href="/my/element/form.html#form-item-slot" class="sidebar-link">Form-Item Slot</a></li><li class="sidebar-sub-header"><a href="/my/element/form.html#form-item-scoped-slot" class="sidebar-link">Form-Item Scoped Slot</a></li><li class="sidebar-sub-header"><a href="/my/element/form.html#form-item-methods" class="sidebar-link">Form-Item Methods</a></li></ul></li><li><a href="/my/element/input.html" class="sidebar-link">Input 输入框</a></li><li><a href="/my/element/input-number.html" class="sidebar-link">InputNumber 计数器</a></li><li><a href="/my/element/radio.html" class="sidebar-link">Radio 单选框</a></li><li><a href="/my/element/rate.html" class="sidebar-link">Rate 评分</a></li><li><a href="/my/element/select.html" class="sidebar-link">Select 选择器</a></li><li><a href="/my/element/slider.html" class="sidebar-link">Slider 滑块</a></li><li><a href="/my/element/switch.html" class="sidebar-link">Switch 开关</a></li><li><a href="/my/element/time-picker.html" class="sidebar-link">TimePicker 时间选择器</a></li><li><a href="/my/element/transfer.html" class="sidebar-link">Transfer 穿梭框</a></li><li><a href="/my/element/upload.html" class="sidebar-link">Upload 上传</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Data</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Notice</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Navigation</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Others</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="form-表单"><a href="#form-表单" class="header-anchor">#</a> Form 表单</h1> <p>由输入框、选择器、单选框、多选框等控件组成，用以收集、校验、提交数据</p> <h2 id="典型表单"><a href="#典型表单" class="header-anchor">#</a> 典型表单</h2> <p>包括各种表单项，比如输入框、选择器、开关、单选框、多选框等。</p> <!----><div class="custom-block tip"><p class="custom-block-title">TIP</p> <p>W3C 标准中有如下<a href="https://www.w3.org/MarkUp/html-spec/html-spec_8.html#SEC8.2" target="_blank" rel="noopener noreferrer">规定<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>：</p> <blockquote><p><i>When there is only one single-line text input field in a form, the user agent should accept Enter in that field as a request to submit the form.</i></p></blockquote> <p>即：当一个 form 元素中只有一个输入框时，在该输入框中按下回车应提交该表单。如果希望阻止这一默认行为，可以在 <code>&lt;el-form&gt;</code> 标签上添加 <code>@submit.native.prevent</code>。</p></div> <h2 id="行内表单"><a href="#行内表单" class="header-anchor">#</a> 行内表单</h2> <p>当垂直方向空间受限且表单较简单时，可以在一行内放置表单。</p> <!----><h2 id="对齐方式"><a href="#对齐方式" class="header-anchor">#</a> 对齐方式</h2> <p>根据具体目标和制约因素，选择最佳的标签对齐方式。</p> <!----><h2 id="表单验证"><a href="#表单验证" class="header-anchor">#</a> 表单验证</h2> <p>在防止用户犯错的前提下，尽可能让用户更早地发现并纠正错误。</p> <!----><h2 id="自定义校验规则"><a href="#自定义校验规则" class="header-anchor">#</a> 自定义校验规则</h2> <p>这个例子中展示了如何使用自定义验证规则来完成密码的二次验证。</p> <!----><div class="custom-block tip"><p class="custom-block-title">TIP</p> <p>自定义校验 callback 必须被调用。 更多高级用法可参考 <a href="https://github.com/yiminghe/async-validator" target="_blank" rel="noopener noreferrer">async-validator<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>。</p></div> <h2 id="动态增减表单项"><a href="#动态增减表单项" class="header-anchor">#</a> 动态增减表单项</h2> <!----><h2 id="数字类型验证"><a href="#数字类型验证" class="header-anchor">#</a> 数字类型验证</h2> <!----><div class="custom-block tip"><p class="custom-block-title">TIP</p> <p>嵌套在 <code>el-form-item</code> 中的 <code>el-form-item</code> 标签宽度默认为零，不会继承 <code>el-form</code> 的 <code>label-width</code>。如果需要可以为其单独设置 <code>label-width</code> 属性。</p></div> <h2 id="表单内组件尺寸控制"><a href="#表单内组件尺寸控制" class="header-anchor">#</a> 表单内组件尺寸控制</h2> <p>通过设置 Form 上的 <code>size</code> 属性可以使该表单内所有可调节大小的组件继承该尺寸。Form-Item 也具有该属性。</p> <!----><h2 id="form-attributes"><a href="#form-attributes" class="header-anchor">#</a> Form Attributes</h2> <table><thead><tr><th>参数</th> <th>说明</th> <th>类型</th> <th>可选值</th> <th>默认值</th></tr></thead> <tbody><tr><td>model</td> <td>表单数据对象</td> <td>object</td> <td>—</td> <td>—</td></tr> <tr><td>rules</td> <td>表单验证规则</td> <td>object</td> <td>—</td> <td>—</td></tr> <tr><td>inline</td> <td>行内表单模式</td> <td>boolean</td> <td>—</td> <td>false</td></tr> <tr><td>label-position</td> <td>表单域标签的位置，如果值为 left 或者 right 时，则需要设置 <code>label-width</code></td> <td>string</td> <td>right/left/top</td> <td>right</td></tr> <tr><td>label-width</td> <td>表单域标签的宽度，例如 '50px'。作为 Form 直接子元素的 form-item 会继承该值。支持 <code>auto</code>。</td> <td>string</td> <td>—</td> <td>—</td></tr> <tr><td>label-suffix</td> <td>表单域标签的后缀</td> <td>string</td> <td>—</td> <td>—</td></tr> <tr><td>hide-required-asterisk</td> <td>是否显示必填字段的标签旁边的红色星号</td> <td>boolean</td> <td>—</td> <td>false</td></tr> <tr><td>show-message</td> <td>是否显示校验错误信息</td> <td>boolean</td> <td>—</td> <td>true</td></tr> <tr><td>inline-message</td> <td>是否以行内形式展示校验信息</td> <td>boolean</td> <td>—</td> <td>false</td></tr> <tr><td>status-icon</td> <td>是否在输入框中显示校验结果反馈图标</td> <td>boolean</td> <td>—</td> <td>false</td></tr> <tr><td>validate-on-rule-change</td> <td>是否在 <code>rules</code> 属性改变后立即触发一次验证</td> <td>boolean</td> <td>—</td> <td>true</td></tr> <tr><td>size</td> <td>用于控制该表单内组件的尺寸</td> <td>string</td> <td>medium / small / mini</td> <td>—</td></tr> <tr><td>disabled</td> <td>是否禁用该表单内的所有组件。若设置为 true，则表单内组件上的 disabled 属性不再生效</td> <td>boolean</td> <td>—</td> <td>false</td></tr></tbody></table> <h2 id="form-methods"><a href="#form-methods" class="header-anchor">#</a> Form Methods</h2> <table><thead><tr><th>方法名</th> <th>说明</th> <th>参数</th></tr></thead> <tbody><tr><td>validate</td> <td>对整个表单进行校验的方法，参数为一个回调函数。该回调函数会在校验结束后被调用，并传入两个参数：是否校验成功和未通过校验的字段。若不传入回调函数，则会返回一个 promise</td> <td>Function(callback: Function(boolean, object))</td></tr> <tr><td>validateField</td> <td>对部分表单字段进行校验的方法</td> <td>Function(props: array | string, callback: Function(errorMessage: string))</td></tr> <tr><td>resetFields</td> <td>对整个表单进行重置，将所有字段值重置为初始值并移除校验结果</td> <td>—</td></tr> <tr><td>clearValidate</td> <td>移除表单项的校验结果。传入待移除的表单项的 prop 属性或者 prop 组成的数组，如不传则移除整个表单的校验结果</td> <td>Function(props: array | string)</td></tr></tbody></table> <h2 id="form-events"><a href="#form-events" class="header-anchor">#</a> Form Events</h2> <table><thead><tr><th>事件名称</th> <th>说明</th> <th>回调参数</th></tr></thead> <tbody><tr><td>validate</td> <td>任一表单项被校验后触发</td> <td>被校验的表单项 prop 值，校验是否通过，错误消息（如果存在）</td></tr></tbody></table> <h2 id="form-item-attributes"><a href="#form-item-attributes" class="header-anchor">#</a> Form-Item Attributes</h2> <table><thead><tr><th>参数</th> <th>说明</th> <th>类型</th> <th>可选值</th> <th>默认值</th></tr></thead> <tbody><tr><td>prop</td> <td>表单域 model 字段，在使用 validate、resetFields 方法的情况下，该属性是必填的</td> <td>string</td> <td>传入 Form 组件的 <code>model</code> 中的字段</td> <td>—</td></tr> <tr><td>label</td> <td>标签文本</td> <td>string</td> <td>—</td> <td>—</td></tr> <tr><td>label-width</td> <td>表单域标签的的宽度，例如 '50px'。支持 <code>auto</code>。</td> <td>string</td> <td>—</td> <td>—</td></tr> <tr><td>required</td> <td>是否必填，如不设置，则会根据校验规则自动生成</td> <td>boolean</td> <td>—</td> <td>false</td></tr> <tr><td>rules</td> <td>表单验证规则</td> <td>object</td> <td>—</td> <td>—</td></tr> <tr><td>error</td> <td>表单域验证错误信息, 设置该值会使表单验证状态变为<code>error</code>，并显示该错误信息</td> <td>string</td> <td>—</td> <td>—</td></tr> <tr><td>show-message</td> <td>是否显示校验错误信息</td> <td>boolean</td> <td>—</td> <td>true</td></tr> <tr><td>inline-message</td> <td>以行内形式展示校验信息</td> <td>boolean</td> <td>—</td> <td>false</td></tr> <tr><td>size</td> <td>用于控制该表单域下组件的尺寸</td> <td>string</td> <td>medium / small / mini</td> <td>-</td></tr></tbody></table> <h2 id="form-item-slot"><a href="#form-item-slot" class="header-anchor">#</a> Form-Item Slot</h2> <table><thead><tr><th>name</th> <th>说明</th></tr></thead> <tbody><tr><td>—</td> <td>Form Item 的内容</td></tr> <tr><td>label</td> <td>标签文本的内容</td></tr></tbody></table> <h2 id="form-item-scoped-slot"><a href="#form-item-scoped-slot" class="header-anchor">#</a> Form-Item Scoped Slot</h2> <table><thead><tr><th>name</th> <th>说明</th></tr></thead> <tbody><tr><td>error</td> <td>自定义表单校验信息的显示方式，参数为 { error }</td></tr></tbody></table> <h2 id="form-item-methods"><a href="#form-item-methods" class="header-anchor">#</a> Form-Item Methods</h2> <table><thead><tr><th>方法名</th> <th>说明</th> <th>参数</th></tr></thead> <tbody><tr><td>resetField</td> <td>对该表单项进行重置，将其值重置为初始值并移除校验结果</td> <td>-</td></tr> <tr><td>clearValidate</td> <td>移除该表单项的校验结果</td> <td>-</td></tr></tbody></table></div> <footer class="page-edit"><!----> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/my/element/datetime-picker.html" class="prev">
        DateTimePicker 日期时间选择器
      </a></span> <span class="next"><a href="/my/element/input.html">
        Input 输入框
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"></div></div>
    <script src="/my/assets/js/app.2dbd0b81.js" defer></script><script src="/my/assets/js/493.009afff8.js" defer></script><script src="/my/assets/js/1311.77560062.js" defer></script><script src="/my/assets/js/1031.8d6cd2c9.js" defer></script>
  </body>
</html>
